package com.server.Tcp;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import static com.server.utils.TcpSocket.getFixedRes;
import static com.server.utils.TcpSocket.writeData;
@Data
@Slf4j
public class TcpTransitMsgString {
    private final static String ip1=getFixedRes("ip1");
    private final static String ip2=getFixedRes("ip2");
    private final static String ip3=getFixedRes("ip3");
    private final static String ip4=getFixedRes("ip4");
    /**
     * 功能：发送数据
     */
    public static void transitMsg(String[] msg){
        try {
            //0为当前
            Map<String,Socket> map=TcpPool.clientMap;
            for (Map.Entry<String,Socket> entry:map.entrySet()){
                //输出流,根据不同的ip选择发送的对象
                BufferedOutputStream bo=new BufferedOutputStream(entry.getValue().getOutputStream());
                //将Map中的连接对象取出，根据事先定好的ip选择哪个发送哪个
                if (ip1.equals(entry.getKey())){
                    log.info("对节点1发送数据");
                    sendMsg(msg[0],bo);
                }else if (ip2.equals(entry.getKey())){
                    log.info("对节点2发送数据");
                    sendMsg(msg[1],bo);
                }else if (ip3.equals(entry.getKey())){
                    log.info("对节点3发送数据");
                    sendMsg(msg[2],bo);
                }else if (ip4.equals(entry.getKey())){
                    log.info("对节点4发送数据");
                    sendMsg(msg[3],bo);
                }else {
                    log.info("有外部连接接入！！！");
                }
/*                switch (entry.getKey()){
                    case ip1:sendMsg(msg[0],bo);break;
                    case ip2:sendMsg(msg[1],bo);break;
                    case ip3:sendMsg(msg[2],bo);break;
                    case ip4:sendMsg(msg[3],bo);break;
                    default:
                }*/
            }
        } catch (IOException e) {
            log.info(e.getMessage());
        }
    }
    public static void transitMsg(String msg,int node) throws IOException {
        Map<String,Socket> map=TcpPool.clientMap;
        BufferedOutputStream bo;
        switch (node) {
            case 1:
                if (map.containsKey(ip1)) {
                    bo = new BufferedOutputStream(map.get(ip1).getOutputStream());
                    sendMsg(msg, bo);
                }else {
                    log.info("1还未建立连接");
                }
                break;
            case 2:
                if (map.containsKey(ip2)){
                    bo=new BufferedOutputStream(map.get(ip2).getOutputStream());
                    sendMsg(msg, bo);
                }else {
                    log.info("2还未建立连接");
                }
                break;
            case 3:
                if (map.containsKey(ip3)) {
                    bo = new BufferedOutputStream(map.get(ip3).getOutputStream());
                    sendMsg(msg, bo);
                }else {
                    log.info("3还未建立连接");
                }
                break;
            case 4:
                if (map.containsKey(ip4)) {
                    bo = new BufferedOutputStream(map.get(ip4).getOutputStream());
                    sendMsg(msg, bo);
                }else {
                    log.info("4还未建立连接");
                }
                break;
            default:
        }
    }
    /**
     * 功能：将待输出的数据输出
     * @param bo 输出缓冲流
     */
    private static void sendMsg(String msg,BufferedOutputStream bo){
        System.err.println("本次发送的数据为："+msg);
        writeData(msg, bo);
    }
}
